home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 12 / Cream of the Crop 12 (Part II) / Cream of the Crop 12 (Part II).iso / OS2 / BLT2_205.ZIP / src / blt2cx02.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-25  |  5.8 KB  |  223 lines

  1. /*
  2.  * 
  3.  * blt2cx02.c - 17-Oct-1995 Cornel Huth 
  4.  * This module is called by blt2demo.c
  5.  * External data (non-BULLET data file) key store
  6.  *
  7.  */
  8.  
  9. #include "platform.h"
  10.  
  11. #ifdef ON_OS2
  12.    #include <os2.h>
  13. #endif
  14. #ifdef ON_W95
  15.    #define WIN32_LEAN_AND_MEAN
  16.    #include <windows.h>
  17. #endif
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <time.h>
  22. #include <string.h>
  23.  
  24. #ifdef ON_OS2
  25.    #include "bullet2.h"
  26. #endif
  27. #ifdef ON_W95
  28.    #include "bullet95.h"
  29. #endif
  30. #ifdef ON_DOSX
  31.    #include "bulletx.h"
  32. #endif
  33.  
  34.  
  35. extern CHAR *collateTable;
  36.  
  37.  
  38. int cx02() {
  39.  
  40. #pragma pack(1)
  41.  
  42. ACCESSPACK AP;
  43. DOSFILEPACK DFP;
  44. CREATEINDEXPACK CIP;
  45. HANDLEPACK HP;
  46. OPENPACK OP;
  47.  
  48. #pragma pack()
  49.  
  50. time_t startTime, endTime;
  51.  
  52. int display = 0;                // display results or not flag
  53. int sameSequence = 1;           // use same sequence for keys or new flag
  54. int binaryKey = 0;              // store key as binary 32-bit flag
  55.  
  56. LONG rndSeqVar = 0;             // used to construct on-the-fly key
  57.  
  58. CHAR nameIX3[] = "$CX02.IX3";   // name of index file created
  59. ULONG indexID=0;                // handle of index file
  60. CHAR keyExpression[128];        // key expression string buffer (159 max)
  61. CHAR keyBuffer[68];             // buffer used to store/receive key values
  62.  
  63. LONG keys2add;                  // keys to store
  64. LONG i;                         // counter
  65. CHAR tmpStr[64];                // misc stuff, non-Bullet related
  66. LONG rez;                       // return value from Bullet
  67. LONG dupHits = 0;               // duplicate keys generated from rand()
  68.  
  69.  
  70. // Delete previous files from any previous run (disregard any error return)
  71.  
  72. DFP.func = DELETE_FILE_DOS;
  73. DFP.filenamePtr = nameIX3;
  74. rez = BULLET(&DFP);
  75.  
  76. strcpy(keyExpression,"EXTERNAL DATA SO NOT PARSED BUT IS STORED");
  77.  
  78. CIP.func = CREATE_INDEX_XB;
  79. CIP.filenamePtr = nameIX3;
  80. CIP.keyExpPtr = keyExpression;
  81. CIP.xbLink = -1;                // must be -1 to use external data
  82.                                 // sort by ASCII (or any valid sort-cmp func)
  83. CIP.sortFunction = (8 << 8) | ASCII_SORT;  // the 8 is the key size
  84.  
  85. CIP.codePage = 0;               // use OS-default code page
  86. CIP.countryCode = 0;            // use OS-default country code
  87. CIP.collatePtr = NULL;          // since ASCII_SORT, no collate table is used
  88. CIP.nodeSize = 512;             // 512-byte node size (or 1024, 2048 bytes)
  89. rez = BULLET(&CIP);
  90. if (rez) {
  91.    printf("Failed index file create.  Err: %li\n",rez);
  92.    goto Abend;
  93. }
  94.  
  95. OP.func = OPEN_INDEX_XB;
  96. OP.filenamePtr = nameIX3;
  97. OP.asMode = READWRITE | DENYNONE; // must always specify a sharing mode
  98. OP.xbLink = -1;                   // must be -1 to use external data
  99. rez = BULLET(&OP);
  100. if (rez) {
  101.    printf("Failed index file open.  Err: %li\n",rez);
  102.    goto Abend;
  103. }
  104. indexID = OP.handle;
  105.  
  106.  
  107. printf("Display keys and key data (slower results)? (y/N) ");
  108. gets(tmpStr);
  109. if (*tmpStr=='y') display = 1;
  110.  
  111. printf("           Repeat same random key sequence? (Y/n) ");
  112. gets(tmpStr);
  113. if (*tmpStr=='n') sameSequence = 0;
  114.  
  115. printf("\n Since storing keys one at a time, it's recommended that you\n");
  116. printf(" limit the keys for this test to less than 100,000 or so.  You could\n");
  117. printf(" add until your disk fills...use Ctrl-C if you can't wait.\n\n");
  118. printf("    How many keys to store (100k takes 1-4 min.)? ");
  119. gets(tmpStr);
  120. keys2add = atol(tmpStr);
  121. if (keys2add < 1) keys2add = 1;       // would you rather end the test?
  122. if (keys2add > 9999999) keys2add = 1; // why wait around for so many?
  123.  
  124. if (sameSequence)
  125.    srand(1);
  126. else
  127.    srand((unsigned)time(0));
  128.  
  129. printf("  Storing %ld keys... ",keys2add);
  130. if (display) printf("\n");
  131.  
  132. time(&startTime);
  133.  
  134. AP.func = STORE_KEY_XB;
  135. AP.handle = indexID;
  136. AP.keyPtr = keyBuffer;
  137. AP.recNo = 1;           // 32-bit data attached, start at 1 (why not)
  138.  
  139. if (binaryKey) {
  140.    printf("binaryKey version is not coded\n");
  141. }
  142. else {
  143.    for (i = 1; i <= keys2add; i++) {
  144.       rndSeqVar = (rand() >> 3) * (rand() >> 3);   // 4095*4095=16769025 max
  145.       sprintf(tmpStr,"%8.8i",rndSeqVar);
  146.       strncpy(keyBuffer,tmpStr,8);
  147.  
  148.       rez = BULLET(&AP);
  149.       if (rez) {
  150.  
  151.          // tried to insert a 'random' key that was already in the index; since
  152.          // this is not INSERT_XB, dups are not automatically handled -- here
  153.          // they're just skipped and another store is done to cover the total
  154.  
  155.          if (rez==EXB_KEY_EXISTS) {
  156.             dupHits++;
  157.             i--;                // do another key store (reuse AP.recNo)
  158.          }
  159.          else {
  160.             printf("Failed storing key %s  %ld.  Err: %li\n",keyBuffer,i,rez);
  161.             goto Abend;
  162.          }
  163.       }
  164.       else {
  165.           if (display) printf("%8.8s  %9.9lu\r", keyBuffer, AP.recNo);
  166.          AP.recNo++;
  167.       }
  168.    }
  169.  
  170. }
  171. time(&endTime);
  172. if (display) printf("\n...");
  173.  
  174. printf("took %lu secs.",(endTime - startTime));
  175.  
  176. if (dupHits) 
  177.    printf(" (had %ld duplicates--ignored)\n",dupHits);
  178. else
  179.    printf("\n");
  180.  
  181. memset(keyBuffer,0,sizeof(keyBuffer)); // gives the \0 to the returned key
  182.  
  183. printf("Accessing %ld keys in order... ",keys2add);
  184. if (display) printf("\n");
  185. time(&startTime);
  186. AP.func = FIRST_KEY_XB;
  187. AP.handle = indexID;
  188. AP.keyPtr = keyBuffer;
  189. rez = BULLET(&AP);
  190. i=0;
  191. while (rez==0) {
  192.    i++;
  193.    if (display) printf("%8.8s  %9.9lu\r", keyBuffer, AP.recNo);
  194.    AP.func = NEXT_KEY_XB;
  195.    rez = BULLET(&AP);
  196. };
  197. if (display) printf("\n...");
  198.  
  199. // expected rez is EXB_END_OF_FILE
  200.  
  201. if (rez!=EXB_END_OF_FILE)  {
  202.    printf("Failed KEY access.  Err: %li\n",rez);
  203.    goto Abend;
  204. }
  205. time(&endTime);
  206. printf("took %lu secs. for %ld keys\n",(endTime - startTime),i);
  207.  
  208.  
  209. // Fatal errors above come straight to here
  210. Abend:
  211.  
  212. // Close file
  213.  
  214. if (indexID) {
  215.    HP.func = CLOSE_INDEX_XB;
  216.    HP.handle = indexID;
  217.    rez = BULLET(&HP);
  218.    if (rez)
  219.       printf("Failed index file close.  Err: %li\n",rez);
  220. }
  221. return rez; 
  222. }
  223.